[#ftl]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
[#assign notNeedKeys = ["lastUpdOprid", "lastUpdName", "lastUpdTm", "createOprid", "createName", "createTm", "orderNum"] /]
[#assign conditionSymbolMap={"eq":"=", "lk":"like"}  verMap={"version":"versionNum", "token":"randomToken", "tokenId":"substr(sys_guid (),1,8)" } /]
[#if table_own?? ]
<mapper namespace="${pack2_dao}">
[#if table_own.sqlInterfaces?? && table_own.sqlInterfaces?size>0]
    <sql id="_select">
        [#list table_own.ownEntity as b]
        ${table_own.tableAlias}.${b.columnCode?lower_case} as ${b.columnJavaCode}[#if b_has_next],[/#if]
        [/#list]
    </sql>
    <sql id="_where">
        [#assign tx=table_own chooseFlag=-1 /]
        [#if tx.whereData?? && tx.whereData?size>0]
        [@whereSqlHandle tx=tx tblAlias='1' blankflag='' /]
        [#else]
        [#list table_own.primaryEntity as b]
        and ${table_own.tableAlias}.${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}
        [/#list]
        [/#if]
    </sql>
[#list table_own.sqlInterfaces as tx]
    [#if tx.needFlag == '1']
    <!-- ${tx.sqlTitle +' '+ tx.sqlDesc!''} -->
    [#if (tx.sqlGenerateType!'') == '1' || (tx.sqlGenerateType!'') == '2']
    [#if tx.sqlCode == 'pageList'][#-- 分页查询集合数据--]
    <sql id="_wherePage">
        [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
        [#list tx.whereData as b]
        <if test="${b.columnJavaCode} != null and ${b.columnJavaCode} != ''" >and ${b.tableAlias}.${b.columnCode?lower_case} ${conditionSymbolMap[b.conditionSymbol]!'='} ${'#{'+b.columnJavaCode+'}'}</if>
        [/#list]
        [#else]
        [#list table_own.ownEntity as b]
        [#if !(notNeedKeys?seq_contains(b.columnJavaCode))]
        <if test="${b.columnJavaCode} != null and ${b.columnJavaCode} != ''" >and ${table_own.tableAlias}.${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}</if>
        [/#if]
        [/#list]
        [/#if]
    </sql>
    <select id="${tx.sqlId}" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          [#if tx.sqlGenerateType == '2' && tx.entityData?? && tx.entityData?size>0]
          [#list tx.entityData as b]
          ${b.tableAlias}.${b.columnCode?lower_case} as ${b.columnJavaCode}[#if b_has_next],[/#if]
          [/#list]
          [#else]
          <include refid="_select"/>
          [/#if]
        [#if tx.sqlGenerateType == '2' && (tx.tableData!'')?length>0]
        ${tx.tableData!''}
        [#else]
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        [/#if]
        <where>
          <include refid="_wherePage"/>
        </where>
    </select>
    [#elseif tx.sqlCode == 'pageCount'][#-- 查询分页总数--]
    <select id="${tx.sqlId}" resultType="java.lang.Integer" parameterType="java.util.Map">
        select 
            count(*)
        [#if tx.sqlGenerateType == '2' && (tx.tableData!'')?length>0]
        ${tx.tableData!''}
        [#else]
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        [/#if]
        <where>
          <include refid="_wherePage"/>
        </where>
    </select>
    
    [#elseif tx.sqlCode == 'selectList'][#-- 查询集合数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entity}" parameterType="java.util.Map">
        [@selectSqlHandle tx=tx tblAlias='2' whereStr='_where' blankflag='' /]
    </select>
    
    [#elseif tx.sqlCode == 'selectOne'][#-- 查询单条数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entity}" parameterType="java.util.Map">
        [@selectSqlHandle tx=tx tblAlias='2' whereStr='_where' blankflag='' /]
    </select>
    
    [#elseif tx.sqlCode == 'selectListEx'][#-- 查询集合数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        [@selectSqlHandle tx=tx tblAlias='2' whereStr='_where' blankflag='' /]
    </select>
    
    [#elseif tx.sqlCode == 'selectOneEx'][#-- 查询单条数据--]
    <select id="${tx.sqlId}" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        [@selectSqlHandle tx=tx tblAlias='2' whereStr='_where' blankflag='' /]
    </select>
    
    [#elseif tx.sqlCode == 'countMap'][#-- 查询总数--]
    <select id="${tx.sqlId}" resultType="java.lang.Integer" parameterType="java.util.Map">
         select 
             count(*)
         [#if tx.sqlGenerateType == '2' && (tx.tableData!'')?length>0]
         ${tx.tableData!''}
         [#else]
         from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
         [/#if]
         <where>
            [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
            [@whereSqlHandle tx=tx tblAlias='2' blankflag='' /]
            [#else]
            <include refid="_where"/>
            [/#if]
         </where>
    </select>
    
    [#elseif tx.sqlCode == 'updMap'][#-- 更新--]
    <update id="${tx.sqlId}">
        update ${table_own.tableCode?lower_case} 
        <set>
          [#list table_own.generalEntity as b]
          <if test="record.${b.columnJavaCode} != null" >${b.columnCode?lower_case} = ${'#{'+'record.'+b.columnJavaCode+'}'},</if>
          [/#list]
        </set>
        <where>
          [#list table_own.ownEntity as b]
          [#if !(notNeedKeys?seq_contains(b.columnJavaCode))]
          <if test="map.${b.columnJavaCode} != null" >and ${b.columnCode?lower_case} = ${'#{'+'map.'+b.columnJavaCode+'}'}</if>
          [/#if]
          [/#list]
        </where> 
    </update>
    
    [#elseif tx.sqlCode == 'deleteMap'][#-- 删除数据--]
    <delete id="${tx.sqlId}" parameterType="java.util.Map">
        delete from ${table_own.tableCode?lower_case}
        <where>
            [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
            [@whereSqlHandle tx=tx tblAlias='0' blankflag='' /]
            [#else]
            [#list table_own.primaryEntity as b]
            and ${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}
            [/#list]
            [/#if]
        </where>
    </delete>
    
    [#elseif tx.sqlCode == 'insert'][#-- 插入单条数据--]
    <insert id="${tx.sqlId}" parameterType="${pack2_entity}">
        insert into ${table_own.tableCode?lower_case} 
        <trim prefix="(" suffix=")" suffixOverrides="," >
          [#list table_own.ownEntity as b]
          <if test="${b.columnJavaCode} != null" >${b.columnCode?lower_case},</if>
          [#if b.columnJavaCode == verMap['version']]
          <if test="${b.columnJavaCode} == null" >${b.columnCode?lower_case},</if>
          [/#if]
          [/#list]
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          [#list table_own.ownEntity as b]
          <if test="${b.columnJavaCode} != null" >${'#{'+b.columnJavaCode+'}'},</if>
          [#if b.columnJavaCode == verMap['version']]
          <if test="${b.columnJavaCode} == null" >0,</if>
          [/#if]
          [/#list]
        </trim>
    </insert>
    
    [#elseif tx.sqlCode == 'update'][#-- 主键更新--]
    [#if table_own.primaryEntity?? && table_own.primaryEntity?size>0]
    <update id="${tx.sqlId}" parameterType="${pack2_entity}">
        update ${table_own.tableCode?lower_case} 
        <set>
          [#list table_own.generalEntity as b]
          <if test="${b.columnJavaCode} != null" >${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'},</if>
          [#if b.columnJavaCode == verMap['version']]
          <if test="${b.columnJavaCode} == null" >${b.columnCode?lower_case} = ${b.columnCode?lower_case} + 1,</if>
          [/#if]
          [/#list]
        </set>
        <where>
            [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
            [@whereSqlHandle tx=tx tblAlias='0' blankflag='' /]
            [#else]
            [#list table_own.primaryEntity as b]
            and ${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}
            [/#list]
            [/#if]
        </where> 
    </update>
    [/#if]
    
    [#else]
    [#if (tx.sqlType!'')?length>0]
    [#if tx.sqlType =="select"]
    <select id="${tx.sqlId}" ${(tx.returnTypeXml??)?then(tx.returnTypeXml,'resultType="'+pack2_entityVo+'"')} ${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,'parameterType="java.util.Map"')}>
    ${tx.tableData!''}
    </select>
    
    [#elseif tx.sqlType =="update"]
    <update id="${tx.sqlId}" ${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,'parameterType="'+pack2_entity+'"')}>
    ${tx.tableData!''}
    </update>
    
    [#elseif tx.sqlType =="insert"]
    <insert id="${tx.sqlId}" ${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,'parameterType="'+pack2_entity+'"')}>
    ${tx.tableData!''}
    </insert>
    
    [#elseif tx.sqlType =="delete"]
    <delete id="${tx.sqlId}" ${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,'parameterType="java.util.Map"')}>
    ${tx.tableData!''}
    </delete>
    
    [/#if]
    
    [#else]
    ${tx.tableData!''}
    [/#if]
    
    [/#if]
    [#elseif (tx.sqlGenerateType!'') == '4']
    [#if (tx.sqlType!'') =="select"]
    <select id="${tx.sqlId}" resultType="${(tx.returnTypeXml??)?then(tx.returnTypeXml,pack2_entityVo)}" parameterType="${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,'java.util.Map')}">
    select
        [#list tx.entityData as b]
        ${b.tableAlias}.${b.columnCode?lower_case} as ${b.columnJavaCode}[#if b_has_next],[/#if]
        [/#list]
    [#if !(tx.tableData!'')?contains('from')]
    from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
    [/#if]
    ${tx.tableData!''}
    </select>
    
    [#elseif (tx.sqlType!'') =="update"]
    <update id="${tx.sqlId}" parameterType="${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,pack2_entity)}">
    </update>
    
    [#elseif (tx.sqlType!'') =="insert"]
    <insert id="${tx.sqlId}" parameterType="${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,pack2_entity)}">
    </insert>
    
    [#elseif (tx.sqlType!'') =="delete"]
    <delete id="${tx.sqlId}" parameterType="${(tx.parameterTypeXml??)?then(tx.parameterTypeXml,'java.util.Map')}">
    </delete>
    
    [/#if]
    
    [#elseif (tx.sqlGenerateType!'') == '3']
    ${tx.tableData!''}
    
    [/#if]
    [/#if]
[/#list]

[#else]
    <sql id="_select">
         [#list table_own.ownEntity as b]
         ${table_own.tableAlias}.${b.columnCode?lower_case} as ${b.columnJavaCode}[#if b_has_next],[/#if]
         [/#list]
    </sql>
    <sql id="_where">
        [#list table_own.primaryEntity as b]
        <if test="${b.columnJavaCode} != null" >and ${table_own.tableAlias}.${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}</if>
        [/#list]
    </sql>
    <sql id="_wherePage">
         [#list table_own.ownEntity as b]
         [#if !(notNeedKeys?seq_contains(b.columnJavaCode))]
         <if test="${b.columnJavaCode} != null and ${b.columnJavaCode} != ''" >and ${table_own.tableAlias}.${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}</if>
         [/#if]
         [/#list]
    </sql>
    
    <!-- 查询分页数据-->
    <select id="selectPageList" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        <where>
          <include refid="_wherePage"/>
        </where>
    </select>
    
    <!-- 查询分页总数-->
    <select id="selectPageCount" resultType="java.lang.Integer" parameterType="java.util.Map">
         select 
             count(*)
         from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
         <where>
           <include refid="_wherePage"/>
         </where>
    </select>
    
    <!-- 查询集合数据-->
    <select id="selectList" resultType="${pack2_entity}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询单条数据-->
    <select id="selectOne" resultType="${pack2_entity}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询集合数据-->
    <select id="selectListEx" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询单条数据-->
    <select id="selectOneEx" resultType="${pack2_entityVo}" parameterType="java.util.Map">
        select
          <include refid="_select"/>
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        <where>
          <include refid="_where"/>
        </where>
    </select>
    
    <!-- 查询总数-->
    <select id="countByMap" resultType="java.lang.Integer" parameterType="java.util.Map">
         select 
             count(*)
         from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
         <where>
           <include refid="_where"/>
         </where>
    </select>
    
    <!-- 更新-->
    <update id="updateByMap">
        update ${table_own.tableCode?lower_case} 
        <set>
          [#list table_own.generalEntity as b]
          <if test="record.${b.columnJavaCode} != null" >${b.columnCode?lower_case} = ${'#{'+'record.'+b.columnJavaCode+'}'},</if>
          [/#list]
        </set>
        <where>
          [#list table_own.ownEntity as b]
          [#if !(notNeedKeys?seq_contains(b.columnJavaCode))]
          <if test="map.${b.columnJavaCode} != null" >and ${b.columnCode?lower_case} = ${'#{'+'map.'+b.columnJavaCode+'}'}</if>
          [/#if]
          [/#list]
        </where> 
    </update>
    
    <!-- 删除数据-->
    <delete id="deleteByMap" parameterType="java.util.Map">
        delete from ${table_own.tableCode?lower_case}
        <where>
          [#list table_own.primaryEntity as b]
          <if test="${b.columnJavaCode} != null" >and ${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}</if>
          [/#list]
        </where>
    </delete>
    
    <!-- 插入单条数据-->
    <insert id="insert" parameterType="${pack2_entity}">
        insert into ${table_own.tableCode?lower_case} 
        <trim prefix="(" suffix=")" suffixOverrides="," >
          [#list table_own.ownEntity as b]
          <if test="${b.columnJavaCode} != null" >${b.columnCode?lower_case},</if>
          [#if b.columnJavaCode == verMap['version']]
          <if test="${b.columnJavaCode} == null" >${b.columnCode?lower_case},</if>
          [/#if]
          [/#list]
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
          [#list table_own.ownEntity as b]
          <if test="${b.columnJavaCode} != null" >${'#{'+b.columnJavaCode+'}'},</if>
          [#if b.columnJavaCode == verMap['version']]
          <if test="${b.columnJavaCode} == null" >0,</if>
          [/#if]
          [/#list]
        </trim>
    </insert>
    [#if table_own.primaryEntity?? && table_own.primaryEntity?size>0]
    
    <!-- 主键更新-->
    <update id="update" parameterType="${pack2_entity}">
        update ${table_own.tableCode?lower_case} 
        <set>
          [#list table_own.generalEntity as b]
          <if test="${b.columnJavaCode} != null" >${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'},</if>
          [#if b.columnJavaCode == verMap['version']]
          <if test="${b.columnJavaCode} == null" >${b.columnCode?lower_case} = ${b.columnCode?lower_case} + 1,</if>
          [/#if]
          [/#list]
        </set>
        <where>
          [#list table_own.primaryEntity as b]
          and ${b.columnCode?lower_case} = ${'#{'+b.columnJavaCode+'}'}
          [/#list]
        </where> 
    </update>
    [/#if]
[/#if]

</mapper>
[/#if]
[#-- SQL条件处理 --]
        [#macro whereSqlHandle tx tblAlias blankflag]
        [#list tx.whereData as b]
        [#if b.conditionSymbolType == '1'][#assign chooseFlag=b_index /][/#if]
        [/#list]
        [#if chooseFlag > -1]
        <choose>
        [#list tx.whereData as b]
        [#assign tableAliasx ='' /]
        [#if tblAlias=="2"][#assign tableAliasx =b.tableAlias+'.' /]
        [#elseif tblAlias=="1"][#assign tableAliasx =table_own.tableAlias+'.' /]
        [/#if]
        [#if b.conditionSymbolType == '1' && chooseFlag != b_index]
        <when test="${b.columnJavaCode} != null">and ${tableAliasx}${b.columnCode?lower_case} ${conditionSymbolMap[b.conditionSymbol]!'='} ${'#{'+b.columnJavaCode+'}'}</when>
        [#elseif b.conditionSymbolType == '1' && chooseFlag == b_index]
        <otherwise>and ${tableAliasx}${b.columnCode?lower_case} ${conditionSymbolMap[b.conditionSymbol]!'='} ${'#{'+b.columnJavaCode+'}'}</otherwise>
        [/#if]
        [/#list]
        </choose>
        [/#if]
        [#list tx.whereData as b]
        [#assign tableAliasx ='' /]
        [#if tblAlias=="2"][#assign tableAliasx =b.tableAlias+'.' /]
        [#elseif tblAlias=="1"][#assign tableAliasx =table_own.tableAlias+'.' /]
        [/#if]
        [#if b.conditionSymbolType == '2']
        and ${tableAliasx}${b.columnCode?lower_case} ${conditionSymbolMap[b.conditionSymbol]!'='} ${'#{'+b.columnJavaCode+'}'}
        [/#if]
        [/#list]
        [#list tx.whereData as b]
        [#assign tableAliasx ='' /]
        [#if tblAlias=="2"][#assign tableAliasx =b.tableAlias+'.' /]
        [#elseif tblAlias=="1"][#assign tableAliasx =table_own.tableAlias+'.' /]
        [/#if]
        [#if b.conditionSymbolType == '3']
        <if test="${b.columnJavaCode} != null" >and ${tableAliasx}${b.columnCode?lower_case} ${conditionSymbolMap[b.conditionSymbol]!'='} ${'#{'+b.columnJavaCode+'}'}</if>
        [/#if]
        [/#list]
        [/#macro]
[#-- 查询SQL处理 --]
        [#macro selectSqlHandle tx tblAlias whereStr  blankflag]
        select
          [#if tx.sqlGenerateType == '2' && tx.entityData?? && tx.entityData?size>0]
          [#list tx.entityData as b]
          ${b.tableAlias}.${b.columnCode?lower_case} as ${b.columnJavaCode}[#if b_has_next],[/#if]
          [/#list]
          [#else]
          <include refid="_select"/>
          [/#if]
        [#if tx.sqlGenerateType == '2' && (tx.tableData!'')?length>0]
        ${tx.tableData!''}
        [#else]
        from ${table_own.tableCode?lower_case} ${table_own.tableAlias}
        [/#if]
        <where>
          [#if tx.sqlGenerateType == '2' && tx.whereData?? && tx.whereData?size>0]
          [@whereSqlHandle tx=tx tblAlias=tblAlias blankflag=blankflag /]
          [#else]
          <include refid="${whereStr}"/>
          [/#if]
        </where>
        [/#macro]